package com.ruoci.redis.controller;

import com.ruoci.redis.utils.RedisUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author yang
 *  Hash数据类型的操作
 **/
@RestController
public class HashController {

    @Resource
    RedisTemplate<String, Object> redisTemplate;


    @GetMapping("/set")
    public void set(){
        redisTemplate.opsForHash().put("serverList", "channelId-123", "clientId-100");
        redisTemplate.opsForHash().put("serverList", "channelId-124", "clientId-100");
        redisTemplate.opsForHash().put("serverList", "channelId-125", "clientId-110");
        redisTemplate.opsForHash().put("serverList", "channelId-126", "clientId-110");
        Map<Object, Object> map = redisTemplate.opsForHash().entries("serverList");
        Map<Object, List<Object>> groupedMap = map.entrySet()
                .stream()
                .collect(Collectors.groupingBy(
                        Map.Entry::getValue,
                        Collectors.mapping(Map.Entry::getKey, Collectors.toList())
                ));
        groupedMap.forEach((value, keys) -> {
            for (Object key : keys) {
                System.out.println(key + ":" + value);
            }
        });
    }


}
